package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.domain.entity.Communication;
import com.example.demo.dto.CommunicationDto;
import com.example.demo.dto.ResponseDto;
import com.example.demo.enums.DiscussStatusEnum;
import com.example.demo.enums.UserIdentifyEnum;
import com.example.demo.exception.ValidatorException;
import com.example.demo.param.CommunicationParam;
import com.example.demo.service.CommunicationService;
import com.example.demo.mapper.CommunicationMapper;
import com.example.demo.utils.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author hspcadmin
* @description 针对表【communication(交流互动)】的数据库操作Service实现
* @createDate 2024-01-12 13:19:54
*/
@Service
public class CommunicationServiceImpl extends ServiceImpl<CommunicationMapper, Communication>
    implements CommunicationService{

    @Resource
    private CommunicationMapper communicationMapper;

    @Override
    public Long save(CommunicationParam param) {
        Long userId = UserInfoShareHolder.getUserInfo().getUser().getId();
        Communication communication = BeanUtil.copyProperties(param, Communication.class);
        communication.setUserId(userId);
        communication.setUserName(UserInfoShareHolder.getUserInfo().getUser().getUsername());
        communication.setUserRealName(UserInfoShareHolder.getUserInfo().getUser().getRealName());

        if (null == param.getLike()){
            communication.setLikeNum(0);
        }
        if (null == param.getFavor()){
            communication.setFavorNum(0);
        }

        communication.setStatus(DiscussStatusEnum.STATUS_TWO.getValue());
        validateTitle(param);
        Date now = DateUtil.date();
        communication.setTime(now);
        communicationMapper.insert(communication);

        return communication.getId();
    }

    /**
     * 校验发布内容
     * @param param
     */
    private void validateTitle(CommunicationParam param){
        // todo 怎么校验
    }

    @Override
    public Page<Communication> list(CommunicationParam param) {
        QueryWrapper<Communication> communicationQueryWrapper = communicationQuery(param);
        communicationQueryWrapper.eq("status",DiscussStatusEnum.STATUS_TWO.getValue());
        Page<Communication> page = new Page<>(param.getPageNumber(), param.getPageSize());
        return communicationMapper.selectPage(page,communicationQueryWrapper);
    }

    @Override
    public void delete(CommunicationParam param) {
        if (null == param.getId()){
            throw new ValidatorException("id不能为空");
        }

        Communication communication = communicationMapper.selectById(param.getId());

        if (null == communication){
            throw new ValidatorException("帖子不存在");
        }

        Long userId = UserInfoShareHolder.getUserInfo().getUser().getId();


        Communication communicationTemp = new Communication();
        communicationTemp.setId(param.getId());
        communicationTemp.setStatus(DiscussStatusEnum.STATUS_THREE.getValue());
        if (UserIdentifyEnum.superAdmin.getIdentify().equals(UserInfoShareHolder.getUserInfo().getUser().getIdentify())
                || UserIdentifyEnum.companyAdmin.getIdentify().equals(UserInfoShareHolder.getUserInfo().getUser().getIdentify())){
            communicationMapper.updateById(communicationTemp);
        }else{
            if (!communication.getUserId().equals(userId)){
                throw new ValidatorException("只能删除自己的帖子");
            }
            communicationMapper.updateById(communicationTemp);
        }

    }


    private QueryWrapper<Communication> communicationQuery(CommunicationParam param){
        QueryWrapper<Communication> queryWrapper = new QueryWrapper<Communication>();

        if (null == param){
            return queryWrapper;
        }

        if (!StringUtils.isEmpty(param.getTitle())){
            queryWrapper.like("title","%"+param.getTitle()+"%");
        }
        if (null != param.getUserId()){
            queryWrapper.eq("user_id",param.getUserId());
        }
        if (!StringUtils.isEmpty(param.getText())){
            queryWrapper.like("text","%"+param.getText()+"%");
        }
        return queryWrapper;
    }
}




